package com.ext.portlet.oeaide.common.action;


import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.liferay.portal.struts.PortletAction;
import com.oeaide.persistence.HQLUtil;
import com.oeaide.persistence.HibernateUtil;
import com.oeaide.persistence.S012Persona;
import com.oeaide.persistence.S013Universidad;
import com.oeaide.persistence.S022Sede;
import com.ext.portlet.oeaide.common.form.BuscarPracticanteForm;
import com.ext.portlet.solicitud.forms.*;


public class BuscarAlumno extends PortletAction{	
	public void processAction(ActionMapping mapping,ActionForm form, PortletConfig config,
			ActionRequest req, ActionResponse res)
	throws Exception
	{		
		BuscarPracticanteForm f = (BuscarPracticanteForm)form;
		List<S012Persona> lista = null;
        if (f.getRadbusqueda().compareTo("Codigo")==0)
        {
        	try
        	{
        		int i = Integer.valueOf(f.getResultadoBusqueda());
        		S012Persona persona = (S012Persona)HibernateUtil.damePorId(S012Persona.class.getName(), i);
                if (persona!=null)
                {
                	lista = new ArrayList<S012Persona>();
                   lista.add(persona);
                }
        	}
        	catch (NumberFormatException ex)
        	{
        		System.out.println(ex.getMessage());
        	}
            
            
        }
        else if (f.getRadbusqueda().compareTo("Nombre")==0)
        {   
        	List<S012Persona> aux = (List<S012Persona>)(HibernateUtil.listarfiltrado(HQLUtil.getAlumnosByName(f.getResultadoBusqueda())));
            if (aux!=null)
            {
            	lista = new ArrayList<S012Persona>();
            	lista.addAll(aux);               
            }
        }
        else if (f.getRadbusqueda().compareTo("ApellidoP")==0)
        {   
        	List<S012Persona> aux = (List<S012Persona>)HibernateUtil.listarfiltrado(HQLUtil.getAlumnosByLastName(f.getResultadoBusqueda()));
            if (aux!=null)
            {
            	lista = new ArrayList<S012Persona>();
            	lista.addAll(aux);          
            }
        }
        else if (f.getRadbusqueda().compareTo("ApellidoM")==0)
        {   
        	List<S012Persona> aux = (List<S012Persona>)HibernateUtil.listarfiltrado(HQLUtil.getAlumnosByMothersLastName(f.getResultadoBusqueda()));
            if (aux!=null)
            {
            	lista = new ArrayList<S012Persona>();
            	lista.addAll(aux);          
            }
        }
		req.getPortletSession().setAttribute("portlet.ext.oeaide.common.resultadoBusqueda",lista);
		setForward(req,"portlet.ext.oeaide.common.buscaralumno");
		
	}
	
	public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig config,
			RenderRequest req, RenderResponse res)
	throws Exception
	{
		
		if (getForward(req) != null && !getForward(req).equals(""))
		{
			return mapping.findForward(getForward(req));
		}
		else
		{
			return mapping.findForward("portlet.ext.oeaide.view");
		}
		
	}
}
